package dev.ece.suit.hexmap;

import java.util.ArrayList;
import java.util.List;

import dev.ece.core.vertex.VAO;
import dev.ece.core.vertex.VBO;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;

@Deprecated
public class HexMapCellVAO {

	private VBO verticeVBO;
	
	private VBO uvVBO;
	
	private VBO normalsVBO;
	
	private VAO vao;
	
	public static HexMapCellVAO createHexMapCellVAO(HexMapCell[][] cells) {
		List<Vector3f> vertice = new ArrayList<Vector3f>();
		List<Vector2f> uv = new ArrayList<Vector2f>();
		List<Vector3f> normals = new ArrayList<Vector3f>();
		int index = 0;
		for(HexMapCell[] row:cells) {
			for(HexMapCell cell: row) {
				vertice.addAll(cell.getVertice());
				uv.addAll(cell.getUv());
				normals.addAll(cell.getNormals());
				cell.setup();
				cell.setFirst(index * cell.getVertice().size());
				index ++;
			}
		}
		HexMapCellVAO vao = new HexMapCellVAO();
		vao.verticeVBO = VBO.newInstance3f(vertice);
		vao.uvVBO = VBO.newInstance2f(uv);
		vao.normalsVBO = VBO.newInstance3f(normals);
		
		vertice.clear();
		uv.clear();
		normals.clear();
		vertice = null;
		uv = null;
		normals = null;
		return vao;
	}
	
	public void initialize() {
		vao = new VAO();
		vao.bind();
		vao.bindVertexArrayBuffer(verticeVBO, 3);
		vao.bindVertexArrayBuffer(uvVBO, 2);
		vao.bindVertexArrayBuffer(normalsVBO, 3);
		vao.finish();
		
		verticeVBO = null;
		uvVBO = null;
		normalsVBO = null;
	}
	
	public void destroy() {
		if(vao != null) {
			vao.destroy();
		}
	}
	
	public void bind() {
		vao.bind();
	}

	public VAO getVao() {
		return vao;
	}
	
}
